//
//  ZJKStatusViewModel.swift
//  MicroBlog
//
//  Created by VAD on 16/4/20.
//  Copyright © 2016年 朱江坤. All rights reserved.
//

import UIKit
import SDWebImage

class ZJKStatusViewModel: NSObject {
    
    var status: ZJKStatus?{
        didSet{
            
            dealMemberImage()
            dealSourceText()
            //设置转发微博的内容
            if let s = status?.retweeted_status{
                
                // @昵称:内容
                if let name = s.user?.name, content = s.text {
                    retweetText = "@\(name):\(content)"
                }
            }
            
            //计算配图视图大小
            retweetPictureViewSize = calcPictureSize(status?.retweeted_status?.pic_urls)
            
            originalPictureViewSize = calcPictureSize(status?.pic_urls)
        }
    }
    
    // 当前微博用户的会员图标
    var memberImage: UIImage?
    // 转发微博的内容
    var retweetText: String?
    
    // 来源字符串
    var sourceText: String?
    
    // 转发微博的配图视图大小
    var retweetPictureViewSize: CGSize = CGSizeZero
    var originalPictureViewSize: CGSize = CGSizeZero
    
    
    // 更新单张图片配图视图的大小
    func updateSingleImagePictureViewSize() {
        retweetPictureViewSize = calcSingleImageSize(status?.retweeted_status?.pic_urls)
        originalPictureViewSize = calcSingleImageSize(status?.pic_urls)
    }
    
    /// 计算单张图片的配图视图大小
    private func calcSingleImageSize(pic_urls: [ZJKStatusPhotoInfo]?) -> CGSize{
        
        // 取出图片地址
        guard let imageUrlString = pic_urls?.first?.thumbnail_pic else {
            return CGSizeZero
        }
        
        // 1. 根据图片的地址，取到已下载好的图片（从缓存里面取）
        if let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(imageUrlString) {
            
            // SDWebImage会根据当前屏幕的缩放比去对图片进行缩放。
            // 如果想要正常显示的话就把其缩放回来
            let scale = UIScreen.mainScreen().scale
            
            var size =  CGSize(width: image.size.width * scale, height: image.size.height * scale)
            
            // 过窄处理
            // 如果宽度小于 50 ，把其图片宽度拉伸到50，高度等比例拉伸
            
            if size.width < 50 {
            
                let height = 50 / size.width * size.height
                
                size.width = 50
                
                size.height = height > 100 ? 100 : height
            }
            return size
        }
        // 2. 计算size并返回
        return CGSizeZero
        
    }

    
    //计算配图视图的大小
    private func calcPictureSize(pic_urls: [ZJKStatusPhotoInfo]?) -> CGSize{
        //获取图片的张数
         let count = pic_urls?.count ?? 0
        
        //如果图片的张数为0时 返回的是CGSizeZero
        if count == 0 {
            return CGSizeZero
        }
        
        //求出多少列
        let col = count == 4 ? 2 : (count > 3 ? 3 : count)
        
        //求出多少行
        let row = ((count - 1) / 3) + 1
        
        let margin: CGFloat = 5
        
        //计算
        let itemWH = (SCREENW - 2 * ZJKStatusCellMargin - 2 * margin) / 3
        
        // 3. 通过每一张图片的大小和列数求出宽度，行数求出高度
        let width = CGFloat(col) * itemWH + CGFloat(col - 1) * margin
        
        let height = CGFloat(row) * itemWH + CGFloat(row - 1) * margin
        
        return CGSize(width: width, height: height)
        
    }

    private func dealSourceText(){
        
        guard let s = status?.source else{
            return
        }
        // 开始截取位置
        if let startIndex = s.rangeOfString("\">")?.endIndex, endIndex = s.rangeOfString("</")?.startIndex {
            let result = s.substringWithRange(startIndex..<endIndex)
            sourceText = "来自 \(result)"
        }
    }
    
    private func dealMemberImage(){
        
        if let rank = status?.user?.mbrank where (rank > 0 && rank < 7) {
            memberImage = UIImage(named: "common_icon_membership_level\(rank)")
        }
    }
}
